/**
 * Copyright (C) 2009 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.orm;

import java.util.List;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cc.aileron.commons.utils.ClassPattrnFinderUtils;
import cc.aileron.orm.bind.G2BinderImpl;
import cc.aileron.orm.bind.G2DaoManagerImpl;
import cc.aileron.orm.factory.serial.G2DaoSerial;
import cc.aileron.orm.sql.template.SqlTemplateConfigure;
import cc.aileron.template.TemplateModule;

import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Module;

/**
 * モジュール
 * 
 * @author Aileron
 */
public abstract class G2DaoModule implements Module
{
    /*
     * (非 Javadoc)
     * 
     * @see com.google.inject.Module#configure(com.google.inject.Binder)
     */
    public void configure(final Binder binder)
    {
        binder.bind(DataSource.class).toInstance(dataSource);
        binder.bind(G2BinderImpl.class).toInstance(g2binder);
        binder.bind(G2DaoManager.class).to(G2DaoManagerImpl.class)
                .asEagerSingleton();
    }

    /**
     * @param dataSource
     * @throws Exception
     */
    public G2DaoModule(final DataSource dataSource) throws Exception
    {
        this.dataSource = dataSource;

        final String name = dataSource.getConnection().getMetaData()
                .getDatabaseProductName();

        logger.trace("dbName : {}", name);

        final G2DbName dbName = G2DbName.convert(name);
        final Module dbModule = new Module()
        {
            @Override
            public void configure(final Binder binder)
            {
                binder.bind(G2DaoSerial.class).to(dbName.getSerial());
            }
        };

        final Module templateModule = new TemplateModule(
                SqlTemplateConfigure.configure);

        g2binder = Guice.createInjector(dbModule, templateModule).getInstance(
                G2BinderImpl.class);

        final List<Class<?>> targetClasses = ClassPattrnFinderUtils
                .getClassNameList(this.getClass().getPackage());

        for (final Class<?> targetClass : targetClasses)
        {
            g2binder.bind(targetClass);
        }
    }

    private final DataSource dataSource;

    private final G2BinderImpl g2binder;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
}
